// import 'package:http/http.dart' as http;


import 'dart:async';

import 'package:qx_global_ads/src/response.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:uuid/uuid.dart';

import 'ad_fetcher.dart';

class AdItemLogic {

   static bool isDebug = true;
   static String platform = 'xiaomi';
   static String packageName = 'com.erushbox.qx_global_ads';
   static String get baseUrl {
     if(isDebug){
       return "http://192.168.1.28:8090";
     }else{
       return "https://advapi.erushbox.pro";
     }
   }
   static void setPlatform(String p) => platform = p;
   static void setPackageName(String p) => packageName = p;
   static void setDebug(bool d) => isDebug = d;
   static AdAppVo? ad;

   static Future<String> loadClientId() async {
     final prefs = await SharedPreferences.getInstance();
     final id = prefs.getString('clientId');
     if(id != null) return id;
     final random = const Uuid().v1();
     await prefs.setString('clientId', random);
     return random;
   }

   static String loadSessionId() => const Uuid().v1();


   static Future<List<AdItem>> fetchAds() async{
     final clientId = await loadClientId(); 
     final sessionId = loadSessionId();
     var tempAd = await AdFetcher.getList(baseUrl, platform, packageName, clientId, sessionId);
     if(tempAd == null) return [];
     ad = tempAd;
     return tempAd.items ?? [];
   }

   static Future<bool> _adClick(String adId) async{
     final clientId = await loadClientId(); 
     final sessionId = loadSessionId();
     var tempAd = await AdFetcher.adClick(baseUrl, platform, packageName, clientId, sessionId, adId);
     if(tempAd == null) return false;
     ad = tempAd;
     return true;
   }

  static Future<bool> _adComplete(String adId) async{
     final clientId = await loadClientId(); 
     final sessionId = loadSessionId();
     var tempAd = await AdFetcher.adComplete(baseUrl, platform, packageName, clientId, sessionId, adId);
     if(tempAd == null) return false;
     ad = tempAd;
     return true;
   }

    static Future <bool> _adClose(String adId) async{
     final clientId = await loadClientId(); 
     final sessionId = loadSessionId();
     var tempAd = await AdFetcher.adClose(baseUrl, platform, packageName, clientId, sessionId, adId);
     if(tempAd == null) return false;
     ad = tempAd;
     return true;
   }

   static int getAdCount() => ad?.items?.length ?? 0;
   static AdItem? getAd() => ad?.items?[currentIndex % getAdCount()];
   static int currentIndex = 0;
   static void addAdIndex() {
     currentIndex = (currentIndex + 1) % getAdCount();
   }

   static void adClick() async{
     var ad = getAd();
     if(ad == null) return;
     await _adClick(ad.id ?? '0');
   }

   static void adComplete() async{
     var ad = getAd();
     if(ad == null) return;
     await _adComplete(ad.id ?? '0');
   }

   static Future<bool> adClose() async{
     var ad = getAd();
     if(ad == null) return false;
     return  await _adClose(ad.id ?? '0');
   }


  
}

